﻿using Manager;
using Manager.Properties;
using Product;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UboxUI;

namespace News
{
    public partial class FrmNewsControl : FrmFather
    {
        public FrmNewsControl()
        {
            InitializeComponent();
            #region 視窗初始化
            this.LblTitle.Text = this.Text;
            this.LblTitle2.Text = LblTitle.Text;
            this.BtnClose.Left = this.Width - this.BtnClose.Width - 10;
            this.BtnNarrow.Left = this.BtnClose.Left - this.BtnNarrow.Width - 10;
            this.panel1.Top = 0;
            this.panel2.Top = this.panel1.Height;
            #endregion
        }

        string conn = Settings.Default.strConn;

        private void Form1_Load(object sender, EventArgs e)
        {
            DataGridViewFill();
        }

        private void DataGridViewFill()
        {
            using (UboxDataClassesDataContext db = new UboxDataClassesDataContext(conn))
            {
                int managerid = 0;
                int newsid = 0;
                int.TryParse(this.txtManagerID.Text, out managerid);
                int.TryParse(this.txtNewsID.Text, out newsid);
                var q = from m in db.news
                        where
                        (chkNewsID.Checked ? newsid == m.news_id : true) &&
                        (chkManagerID.Checked ? managerid == m.manager_id : true) &&
                        (chkDate.Checked ? m.news_date >= this.dtpPulishDate1.Value && m.news_date <= this.dtpPulishDate2.Value : true)
                        select new
                        {
                            編號 = m.news_id,
                            公告標題 = m.news_title,
                            公告內容 = m.news_content,
                            公告管理員 = m.manager_id,
                            公告日期 = m.news_date,
                            可見度 = m.news_visible


                        };
                this.dataGridView1.DataSource = q.ToList();
                this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            DataGridViewFill();
        }


        #region 按鈕的樣式

        private void btnMouseEnter(object sender, EventArgs e)
        {
            ((Button)sender).FlatAppearance.BorderSize = 0;
            ((Button)sender).ForeColor = Color.White;
        }

        private void btnMouseLeave(object sender, EventArgs e)
        {
            ((Button)sender).FlatAppearance.BorderSize = 1;
            ((Button)sender).ForeColor = Color.Black;
        }
        #endregion

        private void btnAddNews_Click(object sender, EventArgs e)
        {
            FrmNewsCreate f = new FrmNewsCreate();
            f.ShowDialog();
            DataGridViewFill();
        }

        private void btnAddNews_MouseEnter(object sender, EventArgs e)
        {
            btnMouseEnter(sender, e);
        }

        private void btnAddNews_MouseLeave(object sender, EventArgs e)
        {
            btnMouseLeave(sender, e);
        }

        private void btnhide_MouseEnter(object sender, EventArgs e)
        {
            btnMouseEnter(sender, e);
        }

        private void btnhide_MouseLeave(object sender, EventArgs e)
        {
            btnMouseLeave(sender, e);
        }

        private void btnSelect_MouseEnter(object sender, EventArgs e)
        {
            btnMouseEnter(sender, e);
        }

        private void btnSelect_MouseLeave(object sender, EventArgs e)
        {
            btnMouseLeave(sender, e);
        }

        private void btnhide_Click(object sender, EventArgs e)
        {
            int newsID = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells["編號"].Value);
            MessageBox.Show("" + newsID);
            UboxDataClassesDataContext db = new UboxDataClassesDataContext(conn);

            Product.news n = db.news.Single(sn => sn.news_id == newsID);
            if (n != null)
            {
                if (n.news_visible == true)
                {
                    n.news_visible = false;
                }
                else
                {
                    n.news_visible = true;
                }
            }
            db.SubmitChanges();
            DataGridViewFill();

            //            Customer cust =
            //    db.Customers.First(c => c.CustomerID == "ALFKI");
            //cust.ContactTitle = "Vice President";
            //db.SubmitChanges();


        }
    }
}
